package com.ruoyi.system.dao;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.ruoyi.common.core.domain.entity.SysDept;

@Repository
public interface SysDeptDAO extends JpaRepository<SysDept, String>,JpaSpecificationExecutor<SysDept>, Serializable {

	public SysDept findTop1findByDeptNameAndParentId(String dtptname,String parentId);
	
	
	@Query(value = "select * from sys_dept where find_in_set(?1, ancestors)", nativeQuery = true)
	public List<SysDept> selectChildrenDeptById(String deptId);
	
	@Modifying
	@Query(value = " update sys_dept set status = '0' where dept_id in (:deptIds)", nativeQuery = true)
	public int updateDeptStatusNormal(@Param("deptIds")List<String> deptIds);
	
	
	
	@Query(value = "select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(?!, ancestors) ", nativeQuery = true)
	public int selectNormalChildrenDeptById(String deptId);
	
	@Query(value = "select count(1) from sys_dept where del_flag = '0' and parent_id = ?1 limit 1 ", nativeQuery = true)
	public int selectChildCountByParentId(String parentId);
	
	
	
	@Query(value = "select count(1) from sys_user where dept_id = ?1 and del_flag = '0' ", nativeQuery = true)
	public int checkDeptExistUser(String deptId);
	
	

}
